<?php

require_once('../lib/dataAccess.php');

//------------------------------------------------------------------
// File: utility.php
// Author: Bryan Frost
// Date Created: 2010-03-25
//
// Shared utility file
//------------------------------------------------------------------

// Return array of GenderIDs and display values.
function GetPlayerGender()
{
    $retVal = array();
    $result = GetQueryResult("SELECT * FROM Gender");
    print('<p id="gender-debug-output">');

    while ($row = mysqli_fetch_array($result) )
    {
        $retVal[$row['GenderID']] = $row['Gender'];
        print('<span>'.$row['GenderID'].' : '.$row['Gender'].'</span>');
    }

    print('</p>');

    return $retVal;
}

// Return array of Tournament GenderIDs and dispaly values.
function GetTournamentGender()	{ return array(1=>"Men's",2=>"Women's",3=>"Coed"); }

// Return array of BeachIDs and display values.
function GetBeaches()			{ return array(1=>'Alki Beach, WA',2=>'Golden Gardens, WA', 3=>'Kitslano Beach, BC'); }

// Return array of YearIDs and display values.
function GetYears()				{ return array(1=>"2010",2=>"2009",3=>"2008",4=>"2007",5=>"2006",6=>"2005",7=>"2004"); }

// Return array of RatingIDs and display values.
function GetRatings()			{ return array(1=>"AAA",2=>"AA",3=>"A",4=>"Unrated"); }

function GetDivisions()			{ return array(1=>"Open",2=>"AAA",3=>"AA",4=>"A",5=>"Novice",6=>"Masters",7=>"Juniors"); }

// Return Gender name based on Id.
function GetTournamentGenderName($genderId)
{
	$array = GetTournamentGender();	
	return $array[$genderId];
}

// Return Rating based on Id.
function GetRatingName($ratingId)
{
	$ratingArray = GetRatings();
	return $ratingArray[$ratingId];
}

function GetDivisionName($divisionId)
{
	$array = GetDivisions();
	return $array[$divisionId];
}

// Compare $eventDate to current season setting.
function IsDateInCurrentSeason($eventDate)
{
    $CURRENT_SEASON = CurrentDate::GetCurrentSeason();
    $firstOfYearString = '-01-01';

    /*
    echo '<p>';
    echo $eventDate.' Is in '.($CURRENT_SEASON).'? '.strtotime($CURRENT_SEASON.$firstOfYearString)
        .' < '.strtotime($eventDate).' < '.strtotime(($CURRENT_SEASON + 1).$firstOfYearString);
    if ( strtotime($eventDate) > strtotime($CURRENT_SEASON.$firstOfYearString) &&
         strtotime($eventDate) < strtotime(($CURRENT_SEASON + 1).$firstOfYearString) )
        echo 'YES';
    echo '</p>';
    */

	return (strtotime($eventDate) > strtotime($CURRENT_SEASON.$firstOfYearString) &&
            $eventDate < strtotime(($CURRENT_SEASON + 1).$firstOfYearString));
}

// Convert date to standardized AVA format (ex. May 30, 2009)
function FormatDate($date)
{
	return date("F d, Y",strtotime($date));
}

// Convert date to standardized AVA format (ex. May 30)
function FormatDateWithoutYear($date)
{
	return date("F d",strtotime($date));	
}

// Convert date to standardized AVA format (ex. Saturday)
function FormatDateGetDayOfWeek($date)
{
	return date("l",strtotime($date));	
}

// Get Day of year
function FormatDateGetDayOfYear($date)
{
	return date("z",strtotime($date));	
}

// Adjust Rating based on time elapsed:
// An earned rating persists through the entire year when it was earned & one year following.
// Rating depreciates by 1 for every successive year.
function AdjustRatingBasedOnTime($eventDate, $rating)
{
	$currentYear = date('Y');

	if ($eventDate > ($currentYear - 2))
		return $rating; 
	elseif ($eventDate > ($currentYear - 3))
		$rating += 1;
	elseif ($eventDate > ($currentYear - 4))
		$rating += 2;
	else
		$rating += 3;
	
	if ($rating > 4)
		$rating = 4;
	
	return $rating;
}

// Calculate Rating based on number of teams and finish
// More teams leads to more over-all ratings earned.
function CalculateRatingEarned($genderId, $divisionId, $numberOfTeams, $result)
{
	// Only Mens (1) and Womens (2) formats award ratings.
	if ($genderId > 2)
		return 4;
	
	// Determin rating for an Open tournament.
	//if ($division == "Open")
	if ($divisionId == 1)
	{
		switch ($numberOfTeams)
		{
			// Large Tournament
			case ($numberOfTeams >= 24):
				if ($result <= 3)
					return 1;
				elseif ($result <= 7)
					return 2;
				elseif ($result <= 13)
					return 3;
				else
					return 4;
				break;
			
			// Medium Tournament
			case  ($numberOfTeams >= 16 && $numberOfTeams < 24):
				if ($result <= 2)
					return 1;
				elseif ($result <= 5)
					return 2;
				elseif ($result <= 9)
					return 3;
				else
					return 4;
				break;
			
			// Small Tournament
			case  ($numberOfTeams >= 8 && $numberOfTeams < 16):
				if ($result <= 1)
					return 1;
				elseif ($result <= 3)
					return 2;
				elseif ($result <= 7)
					return 3;
				else
					return 4;
				break;
				
			// Tiny Tournament
			case  ($numberOfTeams >= 4 && $numberOfTeams < 8):
				if ($result <= 1)
					return 2;
				elseif ($result <= 2)
					return 3;
				else
					return 4;
				break;
		}
	}
	
	// Determin rating for an A tournament (Currently you earn an A rating if you win an A event).
	//elseif ($division == "A")
	elseif ($divisionId == 4)
	{
		if ($result <= 1)
			return 3;
		else
			return 4;
	}
	
	return 4;
}

// Calculate points earned based on tournament format, teams & result.
// Open tournaments award 'double points' (+2 bonus points for winning).
// A tournaments award 'standard points' (+2 bonus points for winning).
// All other divisions do not recieve points.
function CalculatePoints($genderId, $divisionId, $numberOfTeams, $result)
{
	// 1 = Mens, 2 = Womens
	if ($genderId > 2)
		return 0;
	
	switch ($divisionId)
	{
		//case "Open":
		case 1:
			if ($result == 1)
				$points = $numberOfTeams * 2 + $result;
			else
				$points = $numberOfTeams * 2 - $result;
			break;
		//case "A":
		case 4:
			if ($result == 1)
				$points = $numberOfTeams + $result;
			else
				$points = $numberOfTeams - $result;
			break;
		default:
			$points = 0;
			break;
	}
	
	return $points;
}

// Convert an int to its ordinal version (1 => 1st)
function ConvertIntegerToOrdinal($cardinal)
{
	if (!is_numeric($cardinal))
		return;
		//return "Value is not numeric";
	if ($cardinal <= 0)
		return;
		//return "Value must be positive.";
		
	$lastTwoDigits = $cardinal % 100;
	$lastDigit = $lastTwoDigits % 10;
	
//	$suffi;
	switch ($lastDigit)
	{
		case 1:
			$suffix = "st";
			break;
		case 2:
			$suffix = "nd";
			break;
		case 3:
			$suffix = "rd";
			break;
		default:
			$suffix = "th";
			break;
	}
	
	if (11 <= $lastTwoDigits && $lastTwoDigits <= 13)
	{
		$suffix = "th";
	}
	
	return sprintf('%d%s',$cardinal,$suffix);
}

?>